//
//  HomeGuessLikeModel.swift
//  MichaelHuiMall
//
//  Created by apple on 2020/12/14.
//

import UIKit

protocol GuessLikeDelegate:NSObjectProtocol {
    func guessLikeClick(data:NSDictionary)
}

class HomeGuessLikeModel: NSObject {
    
    weak var delegate: GuessLikeDelegate?
    let identifyHL = "HotelListColl"
    var collView : UICollectionView?
    
    var dataSouce : Array<Any>?
    
    
    func bindViewToViewModel(collView : UICollectionView) {
        self.collView = collView
        self.collView?.dataSource = self
        self.collView?.delegate = self
        self.collView?.backgroundColor = UIColor.groupTableViewBackground
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        
        layout.itemSize = .init(width: (SCREEN_WIDTH - 40) / 2, height: (SCREEN_WIDTH - 40) / 2 + 110)
        layout.minimumInteritemSpacing = 8
        layout.minimumLineSpacing = 10
        layout.sectionInset = UIEdgeInsets.init(top: 10, left: 15, bottom: 10, right: 15)
        self.collView?.isScrollEnabled = false
        self.collView?.collectionViewLayout = layout
        self.collView?.showsVerticalScrollIndicator = true
        
        self.collView?.register(UINib.init(nibName: "HotelListColl", bundle: nil), forCellWithReuseIdentifier: identifyHL)
    }
    
    func setUpdata(array:Array<Any>) {
        self.dataSouce = array
        self.collView?.reloadData()
    }

}

extension HomeGuessLikeModel:UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  self.dataSouce?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell : HotelListColl = collectionView.dequeueReusableCell(withReuseIdentifier: identifyHL, for: indexPath) as! HotelListColl
        if let array = self.dataSouce {
            let dic = array[indexPath.row] as? Dictionary<String,Any>
            let firstImg = "\(dic?["firstImg"] ?? "")"
            cell.img.kf.setImage(with: URL.init(string: firstImg))
            cell.titleLab.text = "\(dic?["hotelName"] ?? "")"
            cell.reviewLab.text = "\(dic?["commentCount"] ?? "")人点评"
            
            let ctripCommRate = Double("\(dic?["ctripCommRate"] ?? "0")") ?? 0
            let ctripCommRateStr = String(format: "%.1f",ctripCommRate)
            cell.scoreLab.text = "\(ctripCommRateStr)分"
            cell.priceLab.text = "￥\(dic?["minPrice"] ?? "")"
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        if let array = self.dataSouce {
            let dic = array[indexPath.row] as! NSDictionary
//            delegate?.guessLikeClick(data: dic as NSDictionary)
            
            print("\(dic)")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "GOTOHOTEL"), object: dic)
            

            
            
        }
    }
    
    
    
    
}
